#include "binarysearchdialog.h"
#include <QGridLayout>
#include <QInputDialog>
#include <QDebug>

BinarySearchDialog::BinarySearchDialog(QString algorithm, QString algorithmMovie, QSize size):AlgorithmDialogBase(nullptr, algorithm, algorithmMovie, size)
{

    this->initLayout();

    this->initConnections();

}

BinarySearchDialog::~BinarySearchDialog()
{
}

void BinarySearchDialog::execAlgorithm()
{

    this->strVector.push_back(QStringLiteral("【BinarySearch Begin...】Welcome!"));

    QString dataStr = "";

    int arrLen = this->binSearch.getArrLen();
    for (int i=0;i< arrLen;i++) {
        dataStr += QString::number(this->binSearch.getArr()[i]) + " ";
    }

    qDebug()<<dataStr;

    this->strVector.push_back(QStringLiteral("【BinarySearch DataList】") + dataStr);
    this->strVector.push_back(QStringLiteral("【BinarySearch Target】") + this->binSearch.getTarget());

    int index = -1;
    if(NODIGUI == this->m_type)
    {
        index = this->binSearch.binarySearch(this->binSearch.getArr(), this->binSearch.getArrLen(), this->binSearch.getTarget(), this->strVector);
    }
    else if(DIGUI == this->m_type) {
        index = this->binSearch.binarySearchDigui(this->binSearch.getArr(), 0, this->binSearch.getArrLen(), this->binSearch.getTarget(), this->strVector);
    }

    this->strVector.push_back(QStringLiteral("【BinarySearch Peocess 】: "));

    this->strVector.push_back(QStringLiteral("【BinarySearch Result  】") + QString::number(index));
}

void BinarySearchDialog::initLayout()
{
    //=============================算法形式layout==================================//
    m_pGroupBox = new QGroupBox(this);
    m_pGroupBox->setTitle(tr("选择算法形式"));

    m_pDiGuiBtn = new QRadioButton(tr("递归"));
    m_pNoDiGuiBtn = new QRadioButton(tr("非递归"));
    m_pDiGuiBtn->setChecked(true);

    m_pButtonGroup = new QButtonGroup(this);
    m_pButtonGroup->addButton(m_pDiGuiBtn);
    m_pButtonGroup->addButton(m_pNoDiGuiBtn);
    m_pButtonGroup->setExclusive(true);

    QHBoxLayout *hbox = new QHBoxLayout;
    hbox->addWidget(m_pDiGuiBtn);
    hbox->addWidget(m_pNoDiGuiBtn);
    hbox->addStretch();

    m_pGroupBox->setLayout(hbox);

    //=============================数据来源layout==================================//

    m_pUserGroupBox = new QGroupBox(this);
    m_pUserGroupBox->setTitle(tr("选择数据来源"));

    m_pRandomRadio = new QRadioButton(tr("随机数据"));
    m_pCustomizeRadio = new QRadioButton(tr("自定义数据"));
    m_pRandomRadio->setChecked(true);

    m_pButtonGroup1 = new QButtonGroup(this);
    m_pButtonGroup1->addButton(m_pRandomRadio, 1);
    m_pButtonGroup1->addButton(m_pCustomizeRadio, 0);
    m_pButtonGroup1->setExclusive(true);

    QHBoxLayout *hbox1 = new QHBoxLayout;
    hbox1->addWidget(m_pRandomRadio);
    hbox1->addWidget(m_pCustomizeRadio);
    hbox1->addStretch();

    m_pUserGroupBox->setLayout(hbox1);

    QVBoxLayout *vbox = new QVBoxLayout;
    vbox->addWidget(m_pGroupBox);
    vbox->addWidget(m_pUserGroupBox);
    vbox->addWidget(this->btnDoWork);

    this->setLayout(vbox);
}

void BinarySearchDialog::initConnections()
{
    connect(m_pDiGuiBtn, &QRadioButton::toggled, this, [this]{
        this->m_type = DIGUI;
    });

    connect(m_pNoDiGuiBtn, &QRadioButton::toggled, this, [this]{
        this->m_type = NODIGUI;
    });

    connect(m_pButtonGroup1, QOverload<int>::of(&QButtonGroup::buttonClicked), this, &BinarySearchDialog::onRandomOrCustomizeRadioToggle);
}

void BinarySearchDialog::onRandomOrCustomizeRadioToggle(int index)
{
    if(0==index)
    {
        // 弹出输入框
        QString nums = QInputDialog::getText(this, tr("提示框"), tr("请输入有序数列,并以#分割"),QLineEdit::Normal,"",nullptr, Qt::MSWindowsFixedSizeDialogHint);
        QString pureNums = nums.trimmed();
        QStringList qstrList = pureNums.split("#");

        int arrLen = qstrList.size();
//        int *arr = new int[arrLen];
        float *arr = new float[arrLen];
        this->binSearch.setArr(arr);
        this->binSearch.setArrLen(arrLen);


        for(int idx = 0; idx< this->binSearch.getArrLen(); idx++)
        {
//            this->binSearch.getArr()[idx] = qstrList[idx].toInt();
            this->binSearch.getArr()[idx] = qstrList[idx].toFloat();
        }

        float target = QInputDialog::getText(this, tr("提示框"), tr("请输入要查找的数"),QLineEdit::Normal,"",nullptr, Qt::MSWindowsFixedSizeDialogHint).toInt();
        this->binSearch.setTarget(target);
        // TODO 格式校验
    }
    else if(1==index)
    {
        int arrLen = QInputDialog::getInt(this, tr("提示框"), tr("请输入随机数数量"),5, 5, 102400, 1, nullptr, Qt::MSWindowsFixedSizeDialogHint);

        QString searchNum = QInputDialog::getText(this, tr("提示框"), tr("请输入要查找的数"),QLineEdit::Normal,"",nullptr, Qt::MSWindowsFixedSizeDialogHint);

//        int *arr = new int[arrLen];
        float *arr = new float[arrLen];
        this->binSearch.setArr(arr);
    }
}

void BinarySearchDialog::onTypeComboxGet()
{

}
